'use strict'

var appServices = angular.module('appServices', []);

appServices.factory('messageService', function($timeout){
    return {
        messageList: [],
        addMessage: function (code, msg) {
            var that = this;
            that.messageList.push({code: code, msg: msg});
            $timeout(function () {
                that.messageList.shift();
            }, 1500);
        }
    };
});

appServices.factory('userService', ['$http', function ($http){

	var login = function (user, callback){
		$http.post('/login', user).success(function (data) {
			callback(data);
		});
	};

	var regist = function (userData, callback){
		$http.post('/regist', userData).success(function (data) {
			callback(data);
		});
	};

	return {
		login: login,
		regist: regist
	};
}]);

appServices.factory('postsService', ['$http', function ($http){

	var getPosts = function (name, start, pageNumber, hot, tag, callback){
		if(name){
			var postObj = {
				start: start,
				pageNumber: pageNumber,
				name: name,
				home: 1
			};
		} else {
			var postObj = {
				start: start,
				pageNumber: pageNumber,
				hot: hot,
                tag: tag
			};
		}

		$http.post('/posts', postObj).success(function (data){
			callback(data);
		});
	};

	return {
		getPosts: getPosts
	};

}]);